package com.epam.newsmanagement.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;

import com.epam.newsmanagement.database.dao.INewsDAO;
import com.epam.newsmanagement.database.dao.exception.DAOException;
import com.epam.newsmanagement.model.News;

public final class NewsBean implements Serializable{
	private final static Logger logger = Logger.getLogger(NewsBean.class);
	private static final long serialVersionUID = -1268821519915513157L;
	private static final String LIST_PATH = "newsList";
	private static final String VIEW_PATH = "viewNews";
	private static final String ADD_PATH = "addNews";
	private INewsDAO newsDAO;
	private News currentNews = new News();
	private List<News> newsList;
	private Map<Integer, Boolean> selectedNews = new HashMap<Integer, Boolean>();
	private int currentNewsId;
	private String cancelPath;
	private String currentPath;
	
	
	public News getCurrentNews() {
		return currentNews;
	}
	public void setCurrentNews(News currentNews) {
		this.currentNews = currentNews;
	}
	public List<News> getNewsList() {
		return newsList;
	}
	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}
	public int getCurrentNewsId() {
		return currentNewsId;
	}
	public void setCurrentNewsId(int currentNewsId) {
		this.currentNewsId = currentNewsId;
	}
	public INewsDAO getNewsDAO() {
		return newsDAO;
	}
	public void setNewsDAO(INewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	public Map<Integer, Boolean> getSelectedNews() {
		return selectedNews;
	}
	public void setSelectedNews(Map<Integer, Boolean> selectedNews) {
		this.selectedNews = selectedNews;
	}
	public String getCurrentPath() {
		return currentPath;
	}
	public void setCurrentPath(String currentPath) {
		this.currentPath = currentPath;
	}
	
	@PostConstruct
	private void init(){
		list();
	}
	
	public String delete(){
		try{
			if(currentNewsId != 0){
				newsDAO.remove(new Integer[]{currentNewsId});			
			} else {				
				List<Integer> listToDelete = new ArrayList<Integer>();
				for(Map.Entry<Integer, Boolean> entry : selectedNews.entrySet()){
					if(entry.getValue()){
						listToDelete.add(entry.getKey());
					}
				}
				Integer[] newsToDelete = listToDelete.toArray(new Integer[listToDelete.size()]);
				newsDAO.remove(newsToDelete);
			}
		} catch(DAOException e){
			logger.error(e);
		}
		return list();		
	}
	
	public String list(){
		try {
			newsList = newsDAO.getList();
			currentNewsId = 0;
			selectedNews.clear();
			cancelPath = LIST_PATH;
			setCurrentPath(LIST_PATH);
		} catch (DAOException e) {
			logger.error(e);
		}		
		return LIST_PATH;
	}
	public String view(){
		if(LIST_PATH.equals(currentPath)){
			for(News news : newsList){
				if(currentNewsId == news.getId()){
					currentNews = news;
				}
			}
		}
		cancelPath = VIEW_PATH;
		setCurrentPath(VIEW_PATH);
		return VIEW_PATH;
	}
	
	public String add(){
		currentNews = new News();
		Date currentDate = new Date();
		currentNews.setDate(currentDate);
		setCurrentPath(ADD_PATH);
		return ADD_PATH;
	}
	
	public String edit(){
		if(LIST_PATH.equals(currentPath)){
			for(News news : newsList){
				if(currentNewsId == news.getId()){
					currentNews = news;
				}
			}
		}
		setCurrentPath(ADD_PATH);
		return ADD_PATH;
	}	
	
	public String save(){
		try {
			newsDAO.save(currentNews);
			currentNewsId = currentNews.getId();
		} catch (DAOException e) {
			logger.error(e);
		}		
		return view();
	}
	public String cancel(){
		if(LIST_PATH.equals(cancelPath)){
			return list();
		}
		return view();
	}
}
